package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonContructSatisticsesBean;
import com.fancy.learncenter.bean.CartoonErrorKindsBean;
import com.fancy.learncenter.bean.CartoonStudentStatisBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonErrorKindsAdapter;
import com.fancy.learncenter.ui.view.GridSpacingItemDecoration;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 错题分类
 */
public class CartoonErrorKindsActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<CartoonContructSatisticsesBean> list;
    private CartoonErrorKindsAdapter adapter;
    private String unitId;
    private String homePackageId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_cartoon_error_kinds, Status.STATUS);
        ButterKnife.bind(this);
        getIntentData();
        initView();

    }

    private void getIntentData() {

        Bundle bundle = getIntent().getExtras();
        unitId = bundle.getString(JumpIntentKey.UNIT_ID);
        homePackageId = bundle.getString(JumpIntentKey.HOMEWORK_PACKAGE_ID);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        list = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(2, Utils.dip2pix(this, 20), false);
        recyclerView.addItemDecoration(decoration);
        adapter = new CartoonErrorKindsAdapter(this, list, unitId, homePackageId);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
    }


    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("unitId", unitId);
        map.put("classId", MyApplication.classIDStr);
        HttpMehtod.getInstance().cartoonListErrorKinds(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonErrorKindsBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonErrorKindsBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonErrorKindsBean>() {
                    @Override
                    public void success(CartoonErrorKindsBean dataBean) {

                        adapter.notifyDataSetChanged(dataBean.getHomeworkContructSatistics());
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();
            }
        });
    }

}
